package com.fjeld.client.gui.control.navigation.tree;

import com.fjeld.middleware.jaxb.V1Node;
import com.fjeld.client.gui.screens.ProductionLineView;
import com.fjeld.client.gui.screens.ActionTab;
import com.fjeld.client.gui.screens.node.NodeView;
import com.fjeld.client.gui.control.navigation.NavigationEnum;
import com.fjeld.client.gui.util.factories.LabelFactory;
import com.fjeld.client.gui.TradingFrontEnd;
import com.fjeld.client.util.cache.ClientCacheManager;
import com.fjeld.swingx.JRoundedButton;
import com.explodingpixels.macwidgets.HudWindow;
import com.explodingpixels.macwidgets.HudWidgetFactory;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;

/**
 * Created by IntelliJ IDEA.
 * User: fjelds
 * Date: Mar 12, 2010
 * Time: 11:58:33 PM
 * <p/>
 * Manages the node in the tree.
 */
public class TreeEntryMouseListener extends MouseAdapter implements ActionListener
{
    private static final String ADD = "New child", REMOVE = "Remove", EDIT = "Edit";
    private static final ClientCacheManager CCM = ClientCacheManager.getInstance();

    private V1Node node;
    private TreeEntryPanel treeEntryPanel;
    private JPopupMenu popup;
    private ProductionLineView productionLineView;
    private JPanel contentPanel;


    public TreeEntryMouseListener(TreeEntryPanel treeEntryPanel, V1Node node,
                                  ProductionLineView productionLineView)
    {
        this.treeEntryPanel = treeEntryPanel;
        this.node = node;
        this.productionLineView = productionLineView;

        //Create the popup menu.
        popup = new JPopupMenu();
        JMenuItem menuItem = new JMenuItem(ADD);
        menuItem.addActionListener(this);
        popup.add(menuItem);
        popup.addSeparator();
        menuItem = new JMenuItem(REMOVE);
        menuItem.addActionListener(this);
        popup.add(menuItem);
        menuItem = new JMenuItem(EDIT);
        menuItem.addActionListener(this);
        popup.add(menuItem);
    }

    @Override
    public void mouseClicked(MouseEvent mouseEvent)
    {
        if (mouseEvent.getButton() == MouseEvent.BUTTON1)
        {
            // If left button clicked, show the content node.

            // Create the JPanel on demand.
            if (null == contentPanel)
            {
                V1Node contentNode = CCM.getNode(node.getContentID());
                contentPanel = new NodeView(contentNode);
            }

            productionLineView.displayContent(contentPanel);
            ActionTab.ACTION_TAB.setContentView(NavigationEnum.ACTION_VIEW);
            
        } else if (mouseEvent.getButton() == MouseEvent.BUTTON3)
        {
            // If right button clicked, show the context menu.
            popup.pack();
            popup.revalidate();
            popup.show(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent.getY());
        }
    }

    public void actionPerformed(ActionEvent actionEvent)
    {
        JMenuItem menuItem = (JMenuItem) actionEvent.getSource();
        String command = menuItem.getText();
        if (ADD.equals(command))
        {
            addNode();
        } else if (REMOVE.equals(command))
        {
            removeNode();
        } else if (EDIT.equals(command))
        {
            editNode();
        }
    }

    private void addNode()
    {
        new AddNewEntryDialog("New node", "Enter the name:");
    }

    private void removeNode()
    {
        CCM.removeNode(node);
    }

    private void editNode()
    {

    }

    private class AddNewEntryDialog extends HudWindow implements ActionListener
    {
        private JTextField textField;

        public AddNewEntryDialog(String title, String text)
        {
            super(title);

            JButton ok = new JRoundedButton("OK");
            ok.addActionListener(this);
            JLabel label = LabelFactory.createHeaderLabel(text);
            textField = HudWidgetFactory.createHudTextField("");

            Box box = Box.createVerticalBox();
            box.add(Box.createVerticalStrut(5));
            box.add(label);
            box.add(Box.createVerticalStrut(10));
            box.add(textField);
            box.add(Box.createVerticalStrut(10));
            box.add(ok);
            box.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

            getContentPane().setLayout(new BorderLayout());
            getContentPane().add(box, BorderLayout.NORTH);

            getJDialog().setSize(300, 200);
            getJDialog().setLocationRelativeTo(TradingFrontEnd.frame);
            getJDialog().setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            getJDialog().setVisible(true);
        }

        public void actionPerformed(ActionEvent actionEvent)
        {
            String newNodeName = textField.getText();

            if (newNodeName != null && newNodeName.length() > 0)
            {
                CCM.addTreeChildNodeToParent(node, newNodeName);
            }

            getJDialog().setVisible(false);
        }
    }
}
